package com.fy56.platform.goodsTracking.jdbc.datasource;

import com.dexcoder.dal.JdbcDao;

import com.fy56.platform.goodsTracking.commons.CustomException;
import com.fy56.platform.goodsTracking.model.SysTenant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * Created by XTK on 2016-06-16.
 */
@Repository
public class DBManage {
    @Autowired
    JdbcDao jdbcDao;
    @Autowired
    JdbcDao jdbcDao_upload;
    @Autowired
    JdbcDao jdbcDao_sys;

    private static final Logger log = LoggerFactory.getLogger(DBManage.class);

    public void setDBLink(String dataSource, String databasename) throws CustomException {

        //设置数据库连接
        DBContextHolder.setDBType(dataSource+"-"+databasename);
        try {
            jdbcDao.updateForSql("use " + databasename);
        } catch (Exception e) {
            throw new CustomException("未找到对应数据链接");
        }
    }

    public void setDBLink(String tenant) throws CustomException {

        List<SysTenant> sysTenant = null;
        try {
            sysTenant = jdbcDao_sys.queryListForSql("select sys_Server.DataSource,sys_database.DatabaseName from userinfo left join sys_database on sys_database.DatabaseID=userinfo.databaseid\n" +
                    "left join sys_Server on sys_Server.ServerID=sys_database.ServerID where userinfo.OrganizationCode='" + tenant + "'", SysTenant.class);
        } catch (Exception e) {
            log.error("租户:" + tenant + "连接失败");
            throw new CustomException("数据连接失败");
        }
        if (sysTenant.size() > 1) {
            log.error("租户:" + tenant + "检测到两条或以上设置！");
            throw new CustomException("检测到两条或以上设置！");
        } else if (sysTenant.size() <= 0) {
            log.error("租户:" + tenant + "未检测到服务器相关设置");
            throw new CustomException("未检测到服务器相关设置！");
        }

        String dataSource = sysTenant.get(0).getDatasource();
        String databasename = sysTenant.get(0).getDatabasename();
        //设置数据库连接
        DBContextHolder.setDBType(dataSource+"-"+databasename);
//        try {
//            jdbcDao.updateForSql("use " + databasename);
//        } catch (Exception e) {
//            log.error("租户:" + tenant + "未找到对应数据链接");
//            throw new CustomException("未找到对应数据链接");
//        }
      //  log.info("租户:" + tenant + "数据库" + databasename + "切换成功:" + dataSource);
    }

    public void setDBLink_upload(String tenant) throws CustomException {

        List<SysTenant> sysTenant = null;
        try {
            sysTenant = jdbcDao_sys.queryListForSql("select sys_Server.DataSource,sys_database.DatabaseName from userinfo left join sys_database on sys_database.DatabaseID=userinfo.databaseid\n" +
                    "left join sys_Server on sys_Server.ServerID=sys_database.ServerID where userinfo.OrganizationCode='" + tenant + "'", SysTenant.class);
        } catch (Exception e) {
            log.error("租户:" + tenant + "连接失败");
            throw new CustomException("数据连接失败");
        }
        if (sysTenant.size() > 1) {
            log.error("租户:" + tenant + "检测到两条或以上设置！");
            throw new CustomException("检测到两条或以上设置！");
        } else if (sysTenant.size() <= 0) {
            log.error("租户:" + tenant + "未检测到服务器相关设置");
            throw new CustomException("未检测到服务器相关设置！");
        }

        String dataSource = sysTenant.get(0).getDatasource();
        String databasename = sysTenant.get(0).getDatabasename();
        //设置数据库连接
        DBContextHolder.setDBType(dataSource+"-"+databasename);
//        try {
//            jdbcDao_upload.updateForSql("use " + databasename);
//        } catch (Exception e) {
//            log.error("租户:" + tenant + "未找到对应数据链接");
//            throw new CustomException("未找到对应数据链接");
//        }
        //  log.info("租户:" + tenant + "数据库" + databasename + "切换成功:" + dataSource);
    }
}